package bintree.seachtree;

import bintree.avl.AVLTree;
import bintree.my_bin_tree.BinarySearchTree;
import bintree.rb_tree.RBTree;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 在1000w个随时数的添加上测试普通BST和AVL，红黑树的添加操作性能
 * @author yuisama
 * @date 2022/03/10 11:47
 **/
public class Test1 {
    public static void main(String[] args) {
        int n = 10000000;
        ThreadLocalRandom random = ThreadLocalRandom.current();
        List<Integer> testData = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            testData.add(random.nextInt(Integer.MIN_VALUE,Integer.MAX_VALUE));
        }
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        AVLTree<Integer> avlTree = new AVLTree<>();
        RBTree<Integer> rbTree = new RBTree<>();
        // testBST
        Thread thread1 = new Thread(() -> {
            long start = System.nanoTime();
            for (int i : testData) {
                bst.add(i);
            }
            long end = System.nanoTime();
            System.out.println("bst共耗时: "+(end - start) / 1000000000.0 + "s");
        });
        // testAVL
        Thread thread2 = new Thread(() -> {
            long start = System.nanoTime();
            for (int i : testData) {
                avlTree.add(i);
            }
            long end = System.nanoTime();
            System.out.println("avl共耗时: "+(end - start) / 1000000000.0 + "s");
        });
        // testRBTree
        Thread thread3 = new Thread(() -> {
            long start = System.nanoTime();
            for (int i : testData) {
                rbTree.add(i);
            }
            long end = System.nanoTime();
            System.out.println("RBTree共耗时: "+(end - start) / 1000000000.0 + "s");
        });
        thread1.start();
        thread2.start();
        thread3.start();
    }
}